/**
 * 连续子数组
 * S = 7，nums = [2,3,1,2,4,3]
 * 2   [4,3]
 * @author TANGYE
 * @date 2021/2/8 8:37 下午
 **/
public class HWViewOne {

    public static void main(String[] args) {
        int[] nums = new int[]{1,1,1,1,1,1,1,10};

                //new int[]{2,3,1,2,4,3};
        int s = 7;
        System.out.println(getLength(nums, s));
    }

    public static int getLength(int[] nums, int s){
        if (null == nums || 0 == nums.length){
            return 0;
        }
        int lengthB = nums.length; //存放最短长度
        int lengthN; //存放当前长度
        boolean change = false;
        for (int i = 0; i < nums.length; i++){
            lengthN = 1;
            int sum = nums[i];
            if (sum >= s){
                return 1;
            }
            for (int j = i+1; j < nums.length; j++){
                sum = sum + nums[j];
                lengthN++;
                // 找到满足条件的子数组长度后，和前一次找到的长度比较
                if (sum >= s){
                    if (lengthN < lengthB){
                        change = true;
                        lengthB = lengthN;
                    }
                }
            }
        }

        return !change ? 0 : lengthB;
    }

}
